﻿using Lis.Dto;
using Lis.DTO;
using Lis.IBLL;
using Lis.IRepository;
using Lis.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lis.BLL
{
    public class BorrowBookService : BaseService<BorrowBookDto>, IBorrowBookService
    {
        #region 01，图书借阅+ Model.ServiceResult<string> BorrowBookByBarCode(BorrowBookDto Borrow)
        /// <summary>
        /// 图书借阅
        /// </summary>
        /// <param name="Borrow">借阅数据对象</param>
        /// <returns></returns>
        public ServiceResult<string> BorrowBookByBarCode(BorrowBookDto Borrow)
        {
            //1,验证读者卡号是否在有效期内
            //2，判断读者在DCDCB里面有没有超期图书
            //3，判断读者是否有欠款   FJB
            //4，判断图书是否被借过
            //5，判断图书是否被预约
            //6，判断书是否达到最大可借数量
            //7，判断书是那个馆藏库，根据馆藏判断读者是否达到上线LTQXB,最大可借数量
            //8，然后再日志表里面插借书数据
            //9，借书，在dzdck里面插数据

            #region 1,验证读者卡号是否在有效期内  通过
            var user = ((IUserInfoRepository)(DBSession.SysUserInfoRepository)).GetById(Borrow.UserName);

            if (user==null)//判断是否有这个人
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "查无此人，不能进行借阅操作",
                    Code = "10001"
                };
            }
            if (user.YXRQ == null)//判断有效日期是否为null
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡未设置有效日期，不能进行借阅操作",
                    Code = "10002"
                };
            }
            if (!(DateTime.Now < user.YXRQ))//判断有效日期是否在有效范围内
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡已过有效日期，不能进行借阅操作",
                    Code = "10003"
                };
            }
                
            #endregion

            #region 2，判断读者FJB有没有罚金  通过
            var book_FJ = ((IUserStatusRepository)DBSession.SysUserStatusRepository).GetEntitiesOfUserStatusByUserName(Borrow.UserName).Where<UserStatusDto>(u => u.ZT == "GQ").ToList();
            if (book_FJ != null && book_FJ.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该读者有过期未缴纳罚金，不能进行借阅",
                    Code = "10004"
                };
            }
            #endregion

            #region 3,判断读者DZDCB有没有过期未还图书    通过
            var book_GQ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u => DateTime.Parse(u.YHRQ) < DateTime.Now).ToList();
            if (book_GQ != null && book_GQ.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该读者有过期未还图书，不能进行借阅",
                    Code = "10005"
                };
            }
            #endregion

            #region 4，判断是否重复借阅  通过
            var book_QT = ((ICirculateInfoRepository)DBSession.SysCirculateInfoRepository).GetEntitiesByBarCode(Borrow.BarCode).ToList();
            if (book_QT != null && book_QT.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该图书已被借阅，不能进行重复借阅",
                    Code = "10006"
                };
            }

            //判断是否重复借阅
            var book_CF = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where(u => u.TMH == Borrow.BarCode).ToList();
            if (book_CF != null && book_CF.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该读者已借阅过该图书，不能进行重复借阅",
                    Code = "10007"
                };
            }
            #endregion

            #region 5，判断图书有没有被预约   通过
            //其他人预约
            var book_YY = ((IAppointmentInfoRepository)DBSession.SysAppointmentInfoRepository).GetEntitiesByBarCode(Borrow.BarCode).OrderBy(u => u.YYRQ).ToList();

            if (book_YY != null && book_YY.Count > 0)//有人预约
            {
                //取第一个
                var user_book_YY = book_YY.AsEnumerable().First<AppointmentInfo>();
                if (user_book_YY.DZTM != Borrow.UserName)//判断是不是第一个人预约的
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "该图书已被预约，不能进行借阅操作",
                        Code = "10008"
                    };
                }
            }
            #endregion

            #region 6，判断书是否达到最大可借数量
            //获取该类型的读者的在馆藏地最大借阅数量  通过
            var userCirculateInfo = ((IUserCirculateRepository)DBSession.SysUserCirculateRepository).GetEntitiesByUserType(user.DZLX).First();
            if (userCirculateInfo == null)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "未开通借书权限，不能进行借阅",
                    Code = "10009"
                };
            }

            //根据当前借阅图书 判断最大借阅数量 通过
            var book_WJ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName);
            //判断读者的最大借阅数量是否已达上限  通过
            if (book_WJ.Count() >= userCirculateInfo.ZDJYCS)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "已到最大借阅册数，不能进行借阅",
                    Code = "10010"
                };
            }
            #endregion

            #region 7，判断书是那个馆藏库，根据馆藏判断读者是否达到上线LTQXB,最大可借数量
            //获取图书的馆藏地代码  通过
            var bookDetail = ((IBookDetailRepository)DBSession.SysBookDetailRepository).GetEntitiesByBarCode(Borrow.BarCode).First<BookDetail>();

            //获取该类型的读者的在馆藏地最大借阅数量  通过
            var userCirculateS = ((IUserCirculateRepository)DBSession.SysUserCirculateRepository).GetEntitiesByUserType(user.DZLX, bookDetail.DCDDM);

            if (userCirculateS != null && userCirculateS.Count() <= 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "未开通借书权限，不能进行借阅",
                    Code = "10011"
                };
            }
            var userCirculate = userCirculateS.First();
            //根据借阅图书的馆藏地代码和卡号筛选当前借阅图书  通过
            var book_collection = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u => u.DCD == userCirculate.DCDDM);

            //判断读者的最大借阅数量是否已达上限  通过
            if (book_collection.Count() >= userCirculate.ZDJYCS)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该馆藏已到最大借阅册数，不能进行借阅",
                    Code = "10012"
                };
            }
            #endregion

            #region 8，在日志表里面插数据
            var log = new LogInfo()
               {
                   DZTM = Borrow.UserName,
                   DCTM = Borrow.BarCode,
                   CZY = CirculateConfig.Operator,
                   CZRQ = DateTime.Now,
                   CZLXDM = "WJ",
                   SQLSTR = @"Insert into dzdcb(dztm,dctm,jsrq,yhrq,lyb,dclx,dcd,gcdwdm,jq,ztm,zrz,marc,czy) values ('" + Borrow.UserName + "','" + Borrow.BarCode + "',TO_DATE('" + DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss") + "','YYYY-MM-DD HH24:MI:SS'),TO_DATE('" + DateTime.Now.AddDays(userCirculate.JYTS).ToString("yyyy-mm-dd hh:MM:ss") + "','YYYY-MM-DD HH24:MI:SS'),'zykdck','0001','" + bookDetail.DCDDM + "','1','','" + bookDetail.ZTM + "','" + bookDetail.ZRZ + "','U','system')",
                   LYB = "zydck",
                   MARC = "U",
                   GCDWDM = "1"
               };
            if (DBSession.SysLogInfoRepository.Add(log))
            #endregion
            {
                #region 9，在DZDCB里面插入数据   在Dal里面重新开方法
                var circulateInfo = new CirculateInfo()
                {
                    DZTM = Borrow.UserName,
                    DCTM = Borrow.BarCode,
                    JSRQ = DateTime.Now,
                    YHRQ = DateTime.Now.AddDays(userCirculate.JYTS),
                    LYB = "zykdck",
                    DCLX = "0001",
                    DCD = bookDetail.DCDDM,
                    GCDWDM = "1",
                    ZTM = bookDetail.ZTM,
                    ZRZ = bookDetail.ZRZ,
                    MARC = "U",
                    CZY = CirculateConfig.Operator
                };
                if (DBSession.SysCirculateInfoRepository.Add(circulateInfo))
                {
                    return new ServiceResult<string>()
                    {
                        Success = true,
                        Message = "借书成功",
                        Code = "10000"
                    };
                }
                else
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "操作失败，请重新进行借阅",
                        Code = "10013"
                    };
                }
            }
            else
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "操作失败，请重新进行借阅",
                    Code = "10014"
                };
            }
                #endregion
        }
        #endregion

        #region 02，图书续借+ServiceResult<string> ReNewBookByBarCode(BorrowBookDto Borrow)
        /// <summary>
        /// 图书续借
        /// </summary>
        /// <param name="Borrow">续借对象</param>
        /// <returns></returns>
        public ServiceResult<string> ReNewBookByBarCode(BorrowBookDto Borrow)
        {
            //1，验证读者卡号是否在有效期内

            //2，该书所在的馆藏在读者的流通权限是否允许续借
            //3，判断是否续借过
            //4，判断该书有没有被预约
            //5，读者的所借的书的到期时间是否允许续借
            //6，然后日志表，日志表里面更新数据
            //7，DCDCK里面  更新数据


            #region 1,验证读者卡号是否在有效期内  通过
            var user = ((IUserInfoRepository)(DBSession.SysUserInfoRepository)).GetById(Borrow.UserName);

            if (user == null)//判断是否有这个人
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "查无此人，不能进行借阅操作",
                    Code = "10001"
                };
            }
            if (user.YXRQ == null)//判断有效日期是否为null
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡未设置有效日期，不能进行借阅操作",
                    Code = "10002"
                };
            }
            if (!(DateTime.Now < user.YXRQ))//判断有效日期是否在有效范围内
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡已过有效日期，不能进行借阅操作",
                    Code = "10003"
                };
            }

            #endregion

            #region 2，该书所在的馆藏在读者的流通权限是否允许续借  通过
            //2，该书所在的馆藏在读者的流通权限是否允许续借
            //获取图书的馆藏地代码 
            var bookDetail = ((IBookDetailRepository)DBSession.SysBookDetailRepository).GetEntitiesByBarCode(Borrow.BarCode).First();

            //获取该类型的读者的在馆藏地最大续借数量 
            var userCirculate = ((IUserCirculateRepository)DBSession.SysUserCirculateRepository).GetEntitiesByUserType(user.DZLX, bookDetail.DCDDM).First();

            if (userCirculate == null || userCirculate.XJCS <= 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "续借失败，该读者不能续借该馆藏图书",
                    Code = "10004"
                };
            }
            #endregion

            #region 3,判断读者DZDCB有没有过期未还图书    通过
            var book_GQ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u => DateTime.Parse(u.YHRQ) < DateTime.Now).ToList();
            if (book_GQ != null && book_GQ.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该读者有过期未还图书，不能进行借阅",
                    Code = "10005"
                };
            }
            #endregion

            #region 4，判断是否续借过
            //4，判断是否续借过
            var book_DC = ((ICirculateInfoRepository)DBSession.SysCirculateInfoRepository).GetEntitiesByUserName(Borrow.UserName).Where<CirculateInfo>(u => u.DCTM == Borrow.BarCode).ToList();
            if (book_DC != null && book_DC.Count <= 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "查无此书，不能进行续借操作",
                    Code = "10006"
                };
            }

            //判断是否续借
            if (book_DC[0].XJCS >= userCirculate.XJCS)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "已到达最大续借次数",
                    Code = "10007"
                };
            }
            #endregion

            #region 5，判断图书有没有被预约   通过
            //其他人预约
            var book_YY = ((IAppointmentInfoRepository)DBSession.SysAppointmentInfoRepository).GetEntitiesByBarCode(Borrow.BarCode).OrderBy(u => u.YYRQ).ToList();

            if (book_YY != null && book_YY.Count > 0)//有人预约
            {
                //取第一个
                var user_book_YY = book_YY.AsEnumerable().First<AppointmentInfo>();
                if (user_book_YY.DZTM != Borrow.UserName)//判断是不是第一个人预约的
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "该图书已被预约，不能进行续借操作",
                        Code = "10008"
                    };
                }
            }
            #endregion

            #region 6,读者的所借的书的到期时间是否允许续借    通过
            //6，读者的所借的书的到期时间是否允许续借
            var book_DQ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u =>u.TMH==Borrow.BarCode && DateTime.Parse(u.YHRQ) <= DateTime.Now.AddDays(5)).ToList();
            if (book_DQ != null && book_DQ.Count() <= 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "未到续借时间，不能进行续借操作",
                    Code = "10009"
                };
            }
            #endregion

            #region 7，然后日志表，日志表里面更新数据
            //7，然后日志表，日志表里面更新数据   
            var loanbook = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u => u.TMH == Borrow.BarCode).First();

            var log = new LogInfo()
            {
                DZTM = Borrow.UserName,
                DCTM = Borrow.BarCode,
                CZY = CirculateConfig.Operator,
                CZRQ = DateTime.Now,
                CZLXDM = "XJ",
                SQLSTR = @"UPDATE DZDCB SET XJRQ=TO_DATE('" + DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss") + "','YYYY-MM-DD HH24:MI:SS'),YHRQ=TO_DATE('" + DateTime.Parse(loanbook.YHRQ).AddDays(userCirculate.XJTS).ToString("yyyy-mm-dd hh:MM:ss") + "','YYYY-MM-DD HH24:MI:SS'),XJCS='1' WHERE (DZTM = '" + Borrow.UserName + "') AND (DCTM = '" + Borrow.BarCode + "')",
                LYB = "zydck",
                MARC = "U",
                GCDWDM = "1"
            };
            if (DBSession.SysLogInfoRepository.Add(log))
            #endregion
            {
                book_DC[0].XJCS += 1;
                book_DC[0].YHRQ = book_DC[0].YHRQ.AddDays(userCirculate.XJTS);
                book_DC[0].XJRQ = DateTime.Now;

                #region 8，在DZDCB里面更新数据   在Dal里面重新开方法
                if (DBSession.SysCirculateInfoRepository.Update(book_DC[0]))
                {
                    return new ServiceResult<string>()
                    {
                        Success = true,
                        Message = "续借成功",
                        Code = "10000"
                    };
                }
                else
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "操作失败，请重新进行续借",
                        Code = "10010"
                    };
                }
            }
            else
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "操作失败，请重新进行续借",
                    Code = "100011"
                };
            }
                #endregion

        }
        #endregion

        #region 03，图书转接+ServiceResult<string> LendBookByBarCode(BorrowBookDto Borrow)
        /// <summary>
        /// 图书转接
        /// </summary>
        /// <param name="Borrow">转借对象</param>
        /// <returns></returns>
        public ServiceResult<string> LendBookByBarCode(BorrowBookDto Borrow)
        {
            //1，判断借书人读者卡号是否在有效期内
            //3，判断借书人读者DCDCB有没有超期未还图书
            //2，判断借书人读者有没有欠款（罚金）
            //4，根据要转借图书的馆藏，查询借书人读者是否有借该馆藏库的图书的权限
            //5，是否达到最大可借数量
            //6，根据要转借图书的馆藏，查询借书人读者是否在该馆藏库达到最大借书数量
            //7，判断该书是否被预约（预约的话，判断最早的一个是否是否是转借读者）
            //8，判断该书有没有被转借过
            //9，判断还书人读者卡号是否在有效期内
            //10，判断还书人是否借了这本书
            //11，判断还书人借的该书是否过了还书日期
            //12，日志表里面插入还书记录
            //13，DCDCK删除还书人借书信息
            //14，日志表里面插入借书记录
            //15，DCDCK添加借书人的借书记录

            #region 1,验证读者卡号是否在有效期内  通过
            var user = ((IUserInfoRepository)(DBSession.SysUserInfoRepository)).GetById(Borrow.UserName);

            if (user == null)//判断是否有这个人
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "查无此人，不能进行借阅操作",
                    Code = "10001"
                };
            }
            if (user.YXRQ == null)//判断有效日期是否为null
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡未设置有效日期，不能进行借阅操作",
                    Code = "10002"
                };
            }
            if (!(DateTime.Now < user.YXRQ))//判断有效日期是否在有效范围内
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡已过有效日期，不能进行借阅操作",
                    Code = "10003"
                };
            }

            #endregion

            #region 2,判断读者DZDCB有没有过期未还图书    通过
            //判断读者DZDCB有没有过期未还图书
            var book_GQ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u => DateTime.Parse(u.YHRQ) < DateTime.Now).ToList();
            if (book_GQ != null && book_GQ.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该读者有过期未还图书，不能进行转借操作",
                    Code = "10004"
                };
            }
            #endregion

            #region 3，判断读者FJB有没有罚金  通过
            //判断读者FJB有没有罚金
            var book_FJ = ((IUserStatusRepository)DBSession.SysUserStatusRepository).GetEntitiesOfUserStatusByUserName(Borrow.UserName).Where<UserStatusDto>(u => u.ZT == "GQ").ToList();
            if (book_FJ != null && book_FJ.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该读者有过期未缴纳罚金，不能进行转借操作",
                    Code = "10005"
                };
            }
            #endregion

            #region 4，根据要转借图书的馆藏，查询借书人读者是否有借该馆藏库的图书的权限  通过
            //获取图书的馆藏地代码  通过
            var bookDetail = ((IBookDetailRepository)DBSession.SysBookDetailRepository).GetEntitiesByBarCode(Borrow.BarCode).First<BookDetail>();

            //获取该类型的读者的在馆藏地借阅权限  通过
            var userCirculateS = ((IUserCirculateRepository)DBSession.SysUserCirculateRepository).GetEntitiesByUserType(user.DZLX, bookDetail.DCDDM);
            if (userCirculateS != null && userCirculateS.Count() <= 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "未开通" + bookDetail.DCDMC + "借书权限，不能进行转借",
                    Code = "10006"
                };
            }
            var userCirculate = userCirculateS.First();
            #endregion

            #region 5,是否达到最大可借数量  通过
            //5，是否达到最大可借数量
            var userCirculateInfo = ((IUserCirculateRepository)DBSession.SysUserCirculateRepository).GetEntitiesByUserType(user.DZLX).First();
            if (userCirculateInfo == null)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "未开通借书权限，不能进行借阅",
                    Code = "10007"
                };
            }

            var book_WJ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName);
            if (book_WJ.Count() >= userCirculateInfo.ZDJYCS)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "已到最大借阅册数，不能进行借阅",
                    Code = "10008"
                };
            }
            #endregion

            #region 6，根据要转借图书的馆藏，查询借书人读者是否在该馆藏库达到最大借书数量   通过
            //根据借阅图书的馆藏地代码和卡号筛选当前借阅图书  
            var book_collection = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.UserName).Where<LoanBookDto>(u => u.DCD == userCirculate.DCDDM);

            //判断读者的最大借阅数量是否已达上限  通过
            if (book_collection.Count() >= userCirculate.ZDJYCS)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该馆藏已到最大借阅册数，不能进行借阅",
                    Code = "10009"
                };
            }
            #endregion

            #region 7，判断图书有没有被预约   通过
            //其他人预约
            var book_YY = ((IAppointmentInfoRepository)DBSession.SysAppointmentInfoRepository).GetEntitiesByBarCode(Borrow.BarCode).OrderBy(u => u.YYRQ).ToList();

            if (book_YY != null && book_YY.Count > 0)//有人预约
            {
                //取第一个
                var user_book_YY = book_YY.AsEnumerable().First<AppointmentInfo>();
                if (user_book_YY.DZTM!= Borrow.UserName)//判断是不是第一个人预约的
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "该图书已被预约，不能进行转借操作",
                        Code = "10010"
                    };
                }
            }
            #endregion

            #region 8，判断该书有没有被转借过 通过
            //8，判断是否续借过
            var book_Lend = ((ICirculateInfoRepository)DBSession.SysCirculateInfoRepository).GetEntitiesByUserName(Borrow.ReturnUserName).Where<CirculateInfo>(u => u.DCTM == Borrow.BarCode).ToList();

            if (book_Lend != null && book_Lend.Count > 0)
            {
                //判断是否转借过
                if (book_Lend[0].YQBZ == "1")
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "读者已转借1次,已到达最大转借次数",
                        Code = "10011"
                    };
                }
            }
            #endregion

            #region 9,判断还书人读者卡号是否在有效期内  通过
            //判断还书人读者卡号是否在有效期内
            var Return_user = ((IUserInfoRepository)(DBSession.SysUserInfoRepository)).GetById(Borrow.ReturnUserName);
            if (!(DateTime.Now < user.YXRQ))
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该卡已失效，不能进行转借操作",
                    Code = "10012"
                };
            }
            #endregion

            #region 10，判断还书人是否借了这本书 通过
            //10，判断还书人是否借了这本书
            var book_DC = ((ICirculateInfoRepository)DBSession.SysCirculateInfoRepository).GetEntitiesByUserName(Borrow.ReturnUserName).Where<CirculateInfo>(u => u.DCTM == Borrow.BarCode).ToList();
            if (book_DC != null && book_DC.Count <= 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "查无此书，不能进行转借操作",
                    Code = "10013"
                };
            }
            #endregion

            #region 11,判断还书人借的该书是否过了还书日期    通过
            //根据读者卡号和馆藏号查询该读者的再借图书
            var book_ZJ = ((ICirculateRepository)DBSession.SysCirculateRepository).GetEntitiesOfLoanByUserName(Borrow.ReturnUserName).Where<LoanBookDto>(u => u.TMH == Borrow.BarCode).ToList();

            //判断在借的图书是否是否过了还书期限
            book_ZJ = book_ZJ.AsEnumerable().Where<LoanBookDto>(u => DateTime.Parse(u.YHRQ) < DateTime.Now).ToList();

            if (book_ZJ != null && book_ZJ.Count() > 0)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "该图书已超期，不能进行转借操作",
                    Code = "10014"
                };
            }
            #endregion

            bool exist = false;
            #region 12，日志表里面插入还书记录
            //12，日志表里面插入还书记录
            var logReturn = new LogInfo()
            {
                DZTM = Borrow.ReturnUserName,
                DCTM = Borrow.BarCode,
                CZY = CirculateConfig.Operator,
                CZRQ = DateTime.Now,
                CZLXDM = "HH",
                SQLSTR = @"Delete from dzdcb where dctm='" + Borrow.BarCode + "'",
                LYB = "zydck",
                MARC = "U",
                GCDWDM = "1"
            };
            if (DBSession.SysLogInfoRepository.Add(logReturn))
            #endregion
            {
                #region 13，DCDCK删除还书人借书信息   在Dal里面重新开方法
                var circulateInfo = new CirculateInfo()
                {
                    DZTM = Borrow.ReturnUserName,
                    DCTM = Borrow.BarCode,
                };
                if (DBSession.SysCirculateInfoRepository.Delete(circulateInfo))
                {
                    exist = true;
                }
                #endregion
            }

            #region 判断还书是否成功
            if (!exist)
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "操作失败，请重新进行转接操作",
                    Code = "10015"
                };
            }
            #endregion

            #region 14，在日志表里面插数据
            //14,日志表里面插入借书记录  借书人借书信息
            var logBorrow = new LogInfo()
            {
                DZTM = Borrow.UserName,
                DCTM = Borrow.BarCode,
                CZY = CirculateConfig.Operator,
                CZRQ = DateTime.Now,
                CZLXDM = "WJ",
                SQLSTR = @"Insert into dzdcb(dztm,dctm,jsrq,yhrq,lyb,dclx,dcd,gcdwdm,jq,ztm,zrz,marc,czy) values ('" + Borrow.UserName + "','" + Borrow.BarCode + "',TO_DATE('" + DateTime.Now.ToString("yyyy-mm-dd hh:MM:ss") + "','YYYY-MM-DD HH24:MI:SS'),TO_DATE('" + DateTime.Now.AddDays(userCirculate.JYTS).ToString("yyyy-mm-dd hh:MM:ss") + "','YYYY-MM-DD HH24:MI:SS'),'zykdck','0001','" + bookDetail.DCDDM + "','1','','" + bookDetail.ZTM + "','" + bookDetail.ZRZ + "','U','system')",
                LYB = "zydck",
                MARC = "U",
                GCDWDM = "1"
            };
            if (DBSession.SysLogInfoRepository.Add(logBorrow))
            #endregion
            {
                #region 15，DCDCK添加借书人的借书记录   在Dal里面重新开方法
                var circulateInfo = new CirculateInfo()
                {
                    DZTM = Borrow.UserName,
                    DCTM = Borrow.BarCode,
                    JSRQ = DateTime.Now,
                    YHRQ = DateTime.Now.AddDays(userCirculate.JYTS),
                    LYB = "zykdck",
                    DCLX = "0001",
                    DCD = bookDetail.DCDDM,
                    GCDWDM = "1",
                    ZTM = bookDetail.ZTM,
                    ZRZ = bookDetail.ZRZ,
                    MARC = "U",
                    CZY = CirculateConfig.Operator,
                    YQBZ = "1"
                };
                if (DBSession.SysCirculateInfoRepository.Add(circulateInfo))
                {
                    return new ServiceResult<string>()
                    {
                        Success = true,
                        Message = "转借成功",
                        Code = "10000"
                    };
                }
                else
                {
                    return new ServiceResult<string>()
                    {
                        Success = false,
                        Message = "操作失败，请重新进行转借",
                        Code = "10016"
                    };
                }
            }
            else
            {
                return new ServiceResult<string>()
                {
                    Success = false,
                    Message = "操作失败，请重新进行转借",
                    Code = "10017"
                };
            }
                #endregion
        }
        #endregion

        public override IQueryable<BorrowBookDto> GetPageEntities<S>(int pageSize, int pageIndex, out int total, bool isAsc, System.Linq.Expressions.Expression<Func<BorrowBookDto, bool>> whereLambda, System.Linq.Expressions.Expression<Func<BorrowBookDto, S>> orderByLambda)
        {
            throw new NotImplementedException();
        }

        public override bool Add(BorrowBookDto entity)
        {
            throw new NotImplementedException();
        }

        public override bool UpDate(BorrowBookDto entity)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(BorrowBookDto entity)
        {
            throw new NotImplementedException();
        }
    }
}
